package com.example.layered_arch.example.controller;

import com.example.layered_arch.example.dto.UserRegistrationRequest;
import com.example.layered_arch.example.service.UserRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class UserRegistrationController {

    @Autowired
    private UserRegistrationService userRegistrationService;

    // 接收用户注册信息并处理文件上传
    @PostMapping("/register")
    public Map<String, String> register(@RequestParam("username") String username,
                                        @RequestParam("password") String password,
                                        @RequestParam("phone") String phone,
                                        @RequestParam("email") String email,
                                        @RequestParam("fullName") String fullName,
                                        @RequestParam("gender") String gender,
                                        @RequestParam("birthDate") String birthDate,
                                        @RequestParam("address") String address,
                                        @RequestParam("role") String role,
                                        @RequestParam("avatar") MultipartFile avatar) {
        // 处理文件上传并获取头像URL
        String avatarUrl = handleAvatarUpload(avatar);

        // 创建一个 UserRegistrationRequest 对象并设置各个字段
        UserRegistrationRequest registrationRequest = new UserRegistrationRequest();
        registrationRequest.setUsername(username);
        registrationRequest.setPassword(password);
        registrationRequest.setPhone(phone);
        registrationRequest.setEmail(email);
        registrationRequest.setFullName(fullName);
        registrationRequest.setGender(gender);
        registrationRequest.setBirthDate(birthDate);
        registrationRequest.setAddress(address);
        registrationRequest.setRole(role);
        registrationRequest.setAvatarUrl(avatarUrl);

        // 调用服务层注册方法
        return userRegistrationService.register(registrationRequest);
    }

    // 处理头像上传，保存头像到指定路径并返回访问路径
    private String handleAvatarUpload(MultipartFile avatar) {
        if (avatar != null && !avatar.isEmpty()) {
            // 获取文件名
            String fileName = StringUtils.cleanPath(avatar.getOriginalFilename());

            // 设置文件保存路径为 C:\Users\找到名字了\Desktop\touxiang
            String uploadDir = "C:/Users/找到名字了/Desktop/touxiang";  // 修改为你实际的路径

            try {
                File targetFile = new File(uploadDir, fileName);
                avatar.transferTo(targetFile);  // 将文件保存到目标目录

                // 返回文件的访问路径（可以调整为实际的访问路径）
                return "/images/avatars/" + fileName;  // 假设文件存储在 /images/avatars 目录下
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null; // 如果没有文件上传，返回 null
    }
}
